import { Component, OnInit } from '@angular/core';
import { MemberProfileVo } from 'src/app/model/member-profile-vo';
import { MemberVo } from 'src/app/model/member-vo';
import { MemberStorageService } from 'src/app/services/member-storage.service';
import { MemberService } from 'src/app/services/member.service';
import { Observable } from 'rxjs';
import { Router } from '@angular/router';
import { MemberRecordVo } from 'src/app/model/member-record-vo';
import { BtnNaviSharedService } from 'src/app/services/btn-navi-shared.service';
import { Title } from '@angular/platform-browser';

@Component({
  selector: 'app-personal',
  templateUrl: './personal.component.html',
  styles: [`.rht-link{float:right;display:inline-block;padding:0 1rem}`]
})
export class PersonalComponent implements OnInit {
  public curMember: MemberVo | null;
  //说白了就是在定义的时候它没有具体的初始值，但是确信它在运行期间一定是有值的，这样就规避了这个类型检查。
  public profile$!: Observable<MemberProfileVo>;
  public loginRecord$!: Observable<MemberRecordVo>;

  constructor(
    private title: Title,
    private router: Router,
    private memberStorage: MemberStorageService,
    private memberService: MemberService,
    private btnNaviSharedService: BtnNaviSharedService) {
    this.curMember = this.memberStorage.get();
    this.btnNaviSharedService.active(true);//需要全局的导航
  }

  ngOnInit(): void {
    if (null !==this.curMember && this.curMember) {
      this.title.setTitle('个人中心');
      this.profile$ = this.memberService.getProfile(this.curMember.id);
      this.loginRecord$ = this.memberService.getLoginLastRecord(this.curMember.id, this.curMember.names);
    } else {
      this.router.navigate(['/member/login']);
    }

  }

}
